

#from app import app
'''Hello World'''
'''
@app.route('/')
@app.route('/index')
def index():
    return 'Hello World!'
'''

'''why template:python 返回一个html字符串，太麻烦，难维护...'''

# @app.route('/')
# @app.route('/index')
# def index():
#    user = { 'nickname': 'Miguel' }                                                                                     # fake user(伪造用户)
#    return '''
# <html>
#    <head>
#        <title>Home page</title>
#    </head>
#    <body>
#        <h1>Hello '''+ user['nickname'] + '''</h1>
#    </body>
# </html>'''


#from flask import render_template
from flask_login import login_required
from app import app
from app.models import Posts
'''template'''
@app.route('/')
@app.route('/index')
@login_required
def index():
    '''
    user = {'nickname': 'Miguel'}                                                                                       # fake user(伪造用户)
    '''
    posts = [ #fake array of posts
       {
            'author': { 'nickname': 'John' },
            'body': 'Beautiful day in Portland!'
        },
        {
            'author': { 'nickname': 'Susan' },
            'body': 'The Avengers movie was so cool!'
        }
    ]

    return render_template("index.html", title = "Home Page", posts = posts)

'''web表单'''
from flask import render_template, flash, redirect, url_for
from flask_login import current_user, login_user
from flask import request
from werkzeug.urls import url_parse
from app.models import User
from app.forms import LoginForm

# index view function suppressed for brevity

@app.route('/login', methods = ['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        #flash('Login requested for user {}, remember_me={}'.format(form.username.data, form.remember_me.data))
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title = 'Sign In', form = form)


from flask_login import logout_user
@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))

from app.forms import RegistrationForm
from app import db
@app.route('/register', methods= ['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.emil.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations, you are now a registered user!')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)